package com.liu.servlet;
import com.liu.Bean.User;
import com.liu.dao.impl.UserDaolmpl;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
@WebServlet("/tjyh")
public class Tjyh extends HttpServlet {
    Connection conn = null;
    PreparedStatement rstm = null;
    User user = null;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = new User();
        String username = request.getParameter("username");
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        user.setUsername(username);
        user.setEmail(email);
        user.setPassword(password);
        System.out.println(user);
        UserDaolmpl userDaolmpl = new UserDaolmpl();
        int row = userDaolmpl.register(user);  // 调用 register() 方法进行添加
        System.out.println(row);  // 输出添加用户结果
        if (row > 0){
            request.getRequestDispatcher("yhgl.jsp").forward(request,response);
        }else{
            request.setAttribute("error","用户名或邮件已存在，添加失败");
            response.sendRedirect("tjyh.jsp");
        }
    }
}
